package com.yiyuan.km.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.yiyuan.common.base.BaseDao;
import com.yiyuan.km.dao.IKmDao;
import com.yiyuan.km.model.KmReview;
import com.yiyuan.km.model.KmType;
import com.yiyuan.km.model.Knowledge;

@Repository("kmDao")
public class KmDaoHbImpl extends BaseDao implements IKmDao {
	/** **************知识分类操作***************************** */

	public void addType(KmType kmType) {
		this.getHibernateTemplate().save(kmType);

	}

	public void deleteType(KmType kmType) {
		this.getHibernateTemplate().delete(kmType);

	}

	public KmType getType(Integer typeId) {
		return (KmType) this.getHibernateTemplate().get(KmType.class, typeId);
	}

	public void updateKm(Knowledge knowledge) {
		this.getHibernateTemplate().update(knowledge);
	}

	public void updateType(KmType kmType) {
		this.getHibernateTemplate().update(kmType);

	}

	/** **************知识操作****************************** */
	public void addKm(Knowledge knowledge) {
		this.getHibernateTemplate().save(knowledge);
	}

	public void deleteKm(Knowledge knowledge) {
		this.getHibernateTemplate().delete(knowledge);

	}

	@SuppressWarnings("unchecked")
	public List<KmType> findSubTypeList(String typeId) {
		Session session = this.getSession();
		Query query = session.createQuery("from KmType where typePid=:typeId");
		query.setInteger("typeId", Integer.parseInt(typeId));
		List<KmType> list = query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Knowledge> findKmList(int startIndex, int endIndex) {
		Session session = this.getSession();
		Query query = session.createQuery("from Knowledge");
		query.setFirstResult(startIndex - 1);
		query.setMaxResults(endIndex);
		List<Knowledge> list = query.list();
		return list;
	}

	public Knowledge getKm(Integer kmId) {
		return (Knowledge) this.getHibernateTemplate().get(Knowledge.class,
				kmId);
	}

	public int getKmPageCount() {
		Session session = this.getSession();
		Query query = session.createQuery("select count(*) from Knowledge ");
		int count = Integer.parseInt(query.uniqueResult().toString());
		return count;
	}

	/** **************知识评论操作****************************** */
	public void addReview(KmReview kmReview) {
		this.getHibernateTemplate().save(kmReview);
	}

	public void deleteReview(KmReview kmReview) {
		this.getHibernateTemplate().delete(kmReview);
	}

	public void updateReview(KmReview kmReview) {
		this.getHibernateTemplate().update(kmReview);
	}

	@SuppressWarnings("unchecked")
	public List<KmReview> findReviewList(int startIndex, int endIndex,
			Integer kmId) {
		Session session = this.getSession();
		Query query = session.createQuery("from KmReview where kmId=:kmId");
		query.setInteger("kmId", kmId);
		query.setFirstResult(startIndex - 1);
		query.setMaxResults(endIndex);
		List<KmReview> list = query.list();
		return list;
	}

	public int getReviewPageCount(Integer kmId) {
		Session session = this.getSession();
		Query query = session
				.createQuery("select count(*) from KmReview where kmId=:kmId ");
		query.setInteger("kmId", kmId);
		int count = Integer.parseInt(query.uniqueResult().toString());
		return count;
	}
}
